;(function(document, win, undefined) {
    function Uploading(config){
        this.config = config;
        this.init();
    }
    Uploading.prototype = {
        init: function() {
            this.index = 0;
            this.imgArr = [];
            this.img__btn = document.getElementsByClassName("img__btn")[0];
            this.fileInput = document.getElementsByTagName('input')[0];
            this.box = document.getElementsByClassName("box")[0];
            this.render();
        },
        render: function() {
            var self = this;
            self.img__btn.onclick = function () {
                self.fileInput.click();
            };
            self.fileInput.onchange = function () {
                var count = Math.min((9 - self.index), this.files.length);
                for (var i = 0; i < count; i++) {
                    this.imgCountainer = document.createElement("div");
                    this.imgCountainer.className = "img--item";
                    self.index++;
                    console.log(self.index);
                    this.imgCountainer.onclick = function () {
                        this.outerHTML = '';
                        delete self.imgArr[this['data-index']-1];
                        self.index--;
                        if (self.index < 9) {
                            self.img__btn.style.display = "block";
                        }
                    }
                    self.img = document.createElement("img");
                    self.img.className = "imgCon";
                    console.log(window.URL.createObjectURL(this.files[i]));
                    self.img.src = window.URL.createObjectURL(this.files[i]);
                    self.imgArr.push(this.files[i]);
                    this.imgCountainer['data-index'] = self.imgArr.length;
                    if (self.index > 8) {
                        self.img__btn.style.display = "none";
                        alert("图片最多上传9张");
                    }
                    self.imgOnLoad();
                    this.imgCountainer.appendChild(self.img);
                    self.box.appendChild(this.imgCountainer);
                }
            }
        },
        imgOnLoad:function() {
            this.img.onload = function(){
            var imgSize = [this.width, this.height];
            if (imgSize[0] > imgSize[1]) {
                this.style.height = '3.333333rem';
                this.style.left = -((imgSize[0] * (60 / imgSize[1]) - 60) / 2) / 37.5 + "rem";
            } else {
                this.style.width = '3.333333rem';
                this.style.left = -((imgSize[1] * (60 / imgSize[0]) - 60) / 2) / 37.5 + "rem";
            }
        }
        }
    }
    win['Uploading'] = Uploading;

})(document, window);
     new Uploading();